<!DOCTYPE html>
<html>
<head>
    <title>Electric Dipole Diagram</title>
</head>
<body>
    <canvas id="canvas" width="400" height="500"></canvas>
<script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');

    // Clear canvas with white background
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // --- Configuration ---
    const origin = { x: 200, y: 240 };
    const a = 80; // The distance 'a' in pixels
    const axisPosLen = 140; // Length of positive part of axis from origin
    const axisNegLen = 60;  // Length of negative part of axis from origin
    const arrowSize = 9;
    const chargeRadius = 3;
    const tickLen = 8;
    
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    
    // --- Helper Function to draw arrowheads ---
    function drawArrowHead(x, y, angle) {
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle);
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(-arrowSize, arrowSize / 2.2);
        ctx.moveTo(0, 0);
        ctx.lineTo(-arrowSize, -arrowSize / 2.2);
        ctx.stroke();
        ctx.restore();
    }

    // --- Draw Axes ---
    ctx.lineWidth = 2;

    // z-axis (vertical)
    const zTop = origin.y - axisPosLen;
    const zBottom = origin.y + a + 40; // Extend below -Q charge
    ctx.beginPath();
    ctx.moveTo(origin.x, zBottom);
    ctx.lineTo(origin.x, zTop);
    ctx.stroke();
    drawArrowHead(origin.x, zTop, -Math.PI / 2);

    // y-axis (horizontal)
    const yRight = origin.x + axisPosLen;
    const yLeft = origin.x - axisNegLen;
    ctx.beginPath();
    ctx.moveTo(yLeft, origin.y);
    ctx.lineTo(yRight, origin.y);
    ctx.stroke();
    drawArrowHead(yRight, origin.y, 0);

    // x-axis (diagonal)
    const xAngle = (3 * Math.PI) / 4; // 135 degrees for down-left direction in canvas
    const xVec = { x: Math.cos(xAngle), y: Math.sin(xAngle) };
    const xEnd = { x: origin.x + axisPosLen * 0.9 * xVec.x, y: origin.y + axisPosLen * 0.9 * xVec.y };
    const xStart = { x: origin.x - axisNegLen * xVec.x, y: origin.y - axisNegLen * xVec.y };
    ctx.beginPath();
    ctx.moveTo(xStart.x, xStart.y);
    ctx.lineTo(xEnd.x, xEnd.y);
    ctx.stroke();
    drawArrowHead(xEnd.x, xEnd.y, xAngle);

    // --- Draw Charges ---
    // Positive charge +Q at (0, 0, a)
    const posQY = origin.y - a;
    ctx.beginPath();
    ctx.arc(origin.x, posQY, chargeRadius, 0, 2 * Math.PI);
    ctx.fill();

    // Negative charge -Q at (0, 0, -a)
    const negQY = origin.y + a;
    ctx.beginPath();
    ctx.arc(origin.x, negQY, chargeRadius, 0, 2 * Math.PI);
    ctx.fill();

    // --- Draw Distance Markers ---
    ctx.lineWidth = 1.5;
    const markerOffsetX = -30;

    // Top marker for 'a'
    ctx.beginPath();
    ctx.moveTo(origin.x - tickLen, origin.y);
    ctx.lineTo(origin.x + tickLen, origin.y);
    ctx.moveTo(origin.x - tickLen, posQY);
    ctx.lineTo(origin.x + tickLen, posQY);
    ctx.moveTo(origin.x + markerOffsetX, origin.y);
    ctx.lineTo(origin.x + markerOffsetX, posQY);
    ctx.stroke();

    // Bottom marker for 'a'
    ctx.beginPath();
    ctx.moveTo(origin.x - tickLen, negQY);
    ctx.lineTo(origin.x + tickLen, negQY);
    ctx.moveTo(origin.x + markerOffsetX, origin.y);
    ctx.lineTo(origin.x + markerOffsetX, negQY);
    ctx.stroke();
    
    // --- Draw Labels ---
    // Axis labels
    ctx.font = 'italic 20px Times New Roman';
    ctx.fillText('z', origin.x + 10, zTop - 5);
    ctx.fillText('y', yRight + 8, origin.y + 6);
    ctx.fillText('x', xEnd.x - 20, xEnd.y + 10);
    
    // Distance 'a' labels
    ctx.fillText('a', origin.x + markerOffsetX - 18, origin.y - a / 2 + 6);
    ctx.fillText('a', origin.x + markerOffsetX - 18, origin.y + a / 2 + 6);

    // Charge labels
    ctx.font = '20px Times New Roman';
    ctx.fillText('Q', origin.x + 15, posQY + 6);
    ctx.fillText('-Q', origin.x + 15, negQY + 6);
    
    // Figure Caption
    ctx.font = 'bold 18px Times New Roman';
    ctx.fillText('Fig. 4.28', canvas.width / 2 - 40, canvas.height - 25);

</script>
</body>
</html>